﻿using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using LogWare.AppCode;
using LogWare.Messages;
using LogWare.Properties;

namespace LogWare.ViewModel {
	/// <summary>
	/// This class contains properties that the main View can data bind to.
	/// <para>
	/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
	/// </para>
	/// <para>
	/// You can also use Blend to data bind with the tool's support.
	/// </para>
	/// <para>
	/// See http://www.galasoft.ch/mvvm
	/// </para>
	/// </summary>
	public class MainViewModel : ViewModelBase {

		private OrderViewModel _order;

		private const int MaxColumns = 130;
		private const int MaxLines = 80;


		public OrderViewModel Order {
			get { return _order ?? (_order = new OrderViewModel()); }
			set {
				_order = value;
				RaisePropertyChanged("Order");
			}
		}

		private readonly RelayCommand _optionsCommand =
			new RelayCommand(() => Messenger.Default.Send(new ChildViewMessage<OptionsViewModel>(result => {
				if (result.UserAction == MessageBoxResult.OK) {
					Settings.Default.CompanyAddress = result.UserData.CompanyAddress;
					Settings.Default.CompanyCity = result.UserData.CompanyCity;
					Settings.Default.CompanyName = result.UserData.CompanyName;
					Settings.Default.CompanyPhone = result.UserData.CompanyTelephone;
					Settings.Default.CompanyTaxCode = result.UserData.CompanyTaxCode;
					Settings.Default.CompanyTaxOffice = result.UserData.CompanyTaxOffice;
					Settings.Default.CompanyZip = result.UserData.CompanyZip;
					Settings.Default.Save();
				}
			})));

		public RelayCommand OptionsCommand {
			get { return _optionsCommand; }
		}

		private readonly RelayCommand _printCommand;

		public MainViewModel() {
			_printCommand = new RelayCommand( () => {
				using (ReportWriter writer = new ReportWriter(Settings.Default.OutputDirectory + "\\" + Path.GetRandomFileName() + ".txt", false, Encoding.UTF8) { MaxLineCharacters = MaxColumns}) {
					if(!string.IsNullOrWhiteSpace(Settings.Default.CompanyName)) {
						writer.Write(ReportWriter.AsciiFonts.UpperLeftCorner);
						writer.Write(ReportWriter.AsciiFonts.HorizontalBar, Globals.MaxLegentWidth);
						writer.Write(ReportWriter.AsciiFonts.ConnectionDown);
						writer.Write(ReportWriter.AsciiFonts.HorizontalBar, MaxColumns - Globals.MaxLegentWidth - 3);
						writer.WriteLine(ReportWriter.AsciiFonts.UpperRightCorner);

						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.Write(Settings.Default.CompanyName);
						writer.WriteSpace(Globals.MaxLegentWidth - Settings.Default.CompanyName.Length);
						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.WriteCentered(DateTime.Now.ToShortDateString(), MaxColumns - Globals.MaxLegentWidth - 3);
						writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);

						if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyAddress)) {
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							writer.Write(Settings.Default.CompanyAddress);
							writer.WriteSpace(Globals.MaxLegentWidth - Settings.Default.CompanyAddress.Length);
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							writer.WriteSpace(MaxColumns - Globals.MaxLegentWidth - 3);
							writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
						}
						if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyZip) || !string.IsNullOrWhiteSpace(Settings.Default.CompanyCity)) {
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyZip)) {
								writer.Write("T.K. ");
								writer.Write(Settings.Default.CompanyZip);
								if(!string.IsNullOrEmpty(Settings.Default.CompanyCity)) writer.Write(", ");
							}
							if(!string.IsNullOrWhiteSpace(Settings.Default.CompanyCity)) {
								writer.Write(Settings.Default.CompanyCity);
							}
							writer.WriteSpace(Globals.MaxLegentWidth - (string.IsNullOrEmpty(Settings.Default.CompanyZip) ? 0 : Settings.Default.CompanyZip.Length + 5) -
										 (string.IsNullOrEmpty(Settings.Default.CompanyCity) ? 0 : Settings.Default.CompanyCity.Length) - 2);
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							writer.WriteSpace(MaxColumns - Globals.MaxLegentWidth - 3);
							writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
						}
						if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyPhone)) {
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							writer.Write("Τηλέφωνο ");
							writer.Write(Settings.Default.CompanyPhone);
							writer.WriteSpace(Globals.MaxLegentWidth - Settings.Default.CompanyPhone.Length - 9);
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							writer.WriteSpace(MaxColumns - Globals.MaxLegentWidth - 3);
							writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
						}
						if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyTaxCode) || !string.IsNullOrWhiteSpace(Settings.Default.CompanyTaxOffice)) {
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyTaxCode)) {
								writer.Write("Α.Φ.Μ. ");
								writer.Write(Settings.Default.CompanyTaxCode);
								if (!string.IsNullOrEmpty(Settings.Default.CompanyTaxOffice)) writer.Write(", ");
							}
							if (!string.IsNullOrWhiteSpace(Settings.Default.CompanyTaxOffice)) {
								writer.Write("Δ.Ο.Υ. ");
								writer.Write(Settings.Default.CompanyTaxOffice);
							}
							writer.WriteSpace(Globals.MaxLegentWidth -
							                  (string.IsNullOrEmpty(Settings.Default.CompanyTaxCode)
								                   ? 0 : Settings.Default.CompanyTaxCode.Length + 7) -
							                  (string.IsNullOrEmpty(Settings.Default.CompanyTaxOffice)
								                   ? 0 : Settings.Default.CompanyTaxOffice.Length + 7) - 2);
							writer.Write(ReportWriter.AsciiFonts.VerticalBar);
							writer.WriteSpace(MaxColumns - Globals.MaxLegentWidth - 3);
							writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
						}
						writer.Write(ReportWriter.AsciiFonts.ConnectionRight);
						writer.Write(ReportWriter.AsciiFonts.HorizontalBar, Globals.MaxLegentWidth);
						writer.Write(ReportWriter.AsciiFonts.LowerRightCorner);
						writer.WriteSpace(MaxColumns - Globals.MaxLegentWidth - 3);
						writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);

					} else {
						writer.Write(ReportWriter.AsciiFonts.UpperLeftCorner);
						writer.Write(ReportWriter.AsciiFonts.HorizontalBar, MaxColumns - 2);
						writer.WriteLine(ReportWriter.AsciiFonts.UpperRightCorner);
					}
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.WriteCentered("ΑΠΟΔΕΙΞΗ ΛΙΑΝΙΚΗΣ", MaxColumns-2);
					writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write(ReportWriter.AsciiFonts.ConnectionRight);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, MaxColumns - 2);
					writer.WriteLine(ReportWriter.AsciiFonts.ConnectionLeft);
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, -50}", "Επωνυμία " + Order.Name);
					writer.Write("{0, -40}", "Α.Φ.Μ. " + Order.TaxCode);
					writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, -" + (MaxColumns - 52) + "}", "Επάγγελμα " + Order.Profession);
					writer.Write("{0, -50}", "Δ.Ο.Υ. " + Order.TaxOffice);
					writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, -" + (MaxColumns - 52) + "}", "Διεύθυνση " + Order.Address);
					writer.Write("{0, -50}", "Τ.Κ. " + Order.PostalCode);
					writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, -" + (MaxColumns - 2) + "}", "Πόλη " + Order.City);
					writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write(ReportWriter.AsciiFonts.ConnectionRight);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 90);
					writer.Write(ReportWriter.AsciiFonts.ConnectionDown);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 8);
					writer.Write(ReportWriter.AsciiFonts.ConnectionDown);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 8);
					writer.Write(ReportWriter.AsciiFonts.ConnectionDown);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 7);
					writer.Write(ReportWriter.AsciiFonts.ConnectionDown);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 11);
					writer.WriteLine(ReportWriter.AsciiFonts.ConnectionLeft);

					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, -90}", "Περιγραφή");
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, 8}", "Ποσότητα");
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, 8}", "Τιμή");
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, 7}", "ΦΠΑ (%)");
					writer.Write(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write("{0, 11}", "Τελική Τιμή");
					writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					writer.Write(ReportWriter.AsciiFonts.ConnectionRight);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 90);
					writer.Write(ReportWriter.AsciiFonts.Cross);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 8);
					writer.Write(ReportWriter.AsciiFonts.Cross);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 8);
					writer.Write(ReportWriter.AsciiFonts.Cross);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 7);
					writer.Write(ReportWriter.AsciiFonts.Cross);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 11);
					writer.WriteLine(ReportWriter.AsciiFonts.ConnectionLeft);
					for(int i=0; i<Order.Lines.Count; ++i) {
						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.Write("{0, -90}", Order.Lines[i].Product);
						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.Write("{0, 8}", Order.Lines[i].Quantity);
						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.Write("{0, 8}", Order.Lines[i].Price);
						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.Write("{0, 7}", Order.Lines[i].Vat);
						writer.Write(ReportWriter.AsciiFonts.VerticalBar);
						writer.Write("{0, 11}", Order.Lines[i].LinePrice);
						writer.WriteLine(ReportWriter.AsciiFonts.VerticalBar);
					}
					writer.Write(ReportWriter.AsciiFonts.LowerLeftCorner);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 90);
					writer.Write(ReportWriter.AsciiFonts.ConnectionUp);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 8);
					writer.Write(ReportWriter.AsciiFonts.ConnectionUp);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 8);
					writer.Write(ReportWriter.AsciiFonts.ConnectionUp);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 7);
					writer.Write(ReportWriter.AsciiFonts.ConnectionUp);
					writer.Write(ReportWriter.AsciiFonts.HorizontalBar, 11);
					writer.WriteLine(ReportWriter.AsciiFonts.LowerRightCorner);
					writer.WriteLine();

					writer.Write("{0, -15}", "Καθαρή Αξία:");
					writer.WriteLine("{0:f2}", Order.TotalCost);
					writer.Write("{0, -15}", "Φ.Π.Α.: ");
					writer.WriteLine("{0:f2}", Order.TotalTax);
					writer.Write("{0, -15}", "Συνολική Αξία: ");
					writer.WriteLine("{0:f2}", Order.TotalPrice);
				}			
			});
		}

		public RelayCommand PrintCommand {
			get { return _printCommand; }
		}

		private readonly RelayCommand<TabItem> _closeTabCommand=new RelayCommand<TabItem>(
			(TabItem sender) => {
				if(sender!=null) {
					TabControl tabControl = sender.Parent as TabControl;
					if (tabControl != null) tabControl.Items.Remove(sender);
				}
			}, 
			(TabItem sender) => sender != null
		);

		public RelayCommand<TabItem> CloseTabCommand { get { return _closeTabCommand; } }
	}
}